﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using System.Windows.Forms;
using System.Diagnostics;

namespace PacMan
{
    class CharactersDrawer
    {
        private int _firstFrame = 0;
        private int _CurrFrame = 0;
        private int _lastFrame = 3;
        System.Timers.Timer timer = new System.Timers.Timer(100);

        public CharactersDrawer() 
        {
            timer.Elapsed += timer_Elapsed;
            timer.Enabled = true;
            timer.Start();
        }
        
        public void AnimatePacman(PaintEventArgs e,Pacman pacman) 
        {
            string fullFileName = Environment.GetFullFileName("Images/Pacman.png");
            Bitmap pacmanMoving = new Bitmap(fullFileName);
            var frames = new List<Bitmap>();
            int framesCount = 4;
            int currX = 0;
            
            int _frameHeight = pacmanMoving.Height;
            int _frameWidth = pacmanMoving.Width / framesCount;
            while (currX < pacmanMoving.Width - _frameWidth)
            {
                Rectangle rect = new Rectangle(currX, 0, _frameWidth, _frameHeight);
                frames.Add(pacmanMoving.Clone(rect, pacmanMoving.PixelFormat));
                currX += _frameWidth;
            }

            if (frames.Count > 0)
            {
                if (_CurrFrame >= _lastFrame)
                {
                    _CurrFrame = _firstFrame;
                }
                if (pacman.Position == Position.Up) 
                {
                    frames[_CurrFrame].RotateFlip(RotateFlipType.Rotate90FlipXY);
                }
                if (pacman.Position == Position.Down)
                {
                    frames[_CurrFrame].RotateFlip(RotateFlipType.Rotate90FlipX);
                }
                if (pacman.Position == Position.Left)
                {
                    frames[_CurrFrame].RotateFlip(RotateFlipType.Rotate180FlipY);
                }
                try
                {
                    e.Graphics.DrawImage(frames[_CurrFrame], pacman.X, pacman.Y);
                }
                catch(ArgumentOutOfRangeException) 
                {
                    _CurrFrame = _firstFrame;
                }
            }
        }

        public void timer_Elapsed(object sender, EventArgs e) 
        {
            _CurrFrame++;
            timer.Start();
        }
        
        
        public void DrawPacman(Pacman pacman,PaintEventArgs e)
        {    
                AnimatePacman(e, pacman);

                if (pacman.Position == Position.Stay)
                {
                    string fullFileName = Environment.GetFullFileName("Images/PacmanClosed.png");
                    e.Graphics.DrawImage(new Bitmap(fullFileName), pacman.X, pacman.Y);
                }

        }

        public void DrawInky(Inky inky, PaintEventArgs e)
        {
            if (inky.Position == Position.Right || inky.Position == Position.Up)
            {

                string fullFileName = Environment.GetFullFileName("Images/InkyRight.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), inky.X, inky.Y);
                return;
            }

            if (inky.Position == Position.Left || inky.Position == Position.Down || inky.Position == Position.Stay)
            {
                string fullFileName = Environment.GetFullFileName("Images/InkyLeft.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), inky.X, inky.Y);
                return;
            }
        }

        public void DrawClyde(Clyde clyde, PaintEventArgs e)
        {
            if (clyde.Position == Position.Right || clyde.Position == Position.Up)
            {
                string fullFileName = Environment.GetFullFileName("Images/ClydeRight.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), clyde.X, clyde.Y);
                return;
    
            }
            
            if (clyde.Position == Position.Left || clyde.Position == Position.Down || clyde.Position == Position.Stay)
            {
                string fullFileName = Environment.GetFullFileName("Images/ClydeLeft.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), clyde.X, clyde.Y);
                return;
            }
        }
        
        public void DrawPinky(Pinky pinky,PaintEventArgs e)
        {
            if (pinky.Position == Position.Right || pinky.Position == Position.Up)
            {
                string fullFileName = Environment.GetFullFileName("Images/PinkyRight.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), pinky.X, pinky.Y);
                return;
            }
            
            if (pinky.Position == Position.Left || pinky.Position == Position.Down || pinky.Position == Position.Stay)
            {
                string fullFileName = Environment.GetFullFileName("Images/PinkyLeft.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), pinky.X, pinky.Y);
                return;
            }

            
        }
        
        public void DrawBlinky(Blinky blinky, PaintEventArgs e) 
        {
            if (blinky.Position == Position.Right || blinky.Position == Position.Up)
            {
                string fullFileName = Environment.GetFullFileName("Images/BlinkyRight.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), blinky.X, blinky.Y);
                return;
            }
            
            if (blinky.Position == Position.Left || blinky.Position == Position.Down)
            {
                string fullFileName = Environment.GetFullFileName("Images/BlinkyRight.png");
                e.Graphics.DrawImage(new Bitmap(fullFileName), blinky.X, blinky.Y);
                return;
            }
           
        }

        public void DrawBonus(Bonus bonus, PaintEventArgs e)
        {
            if (bonus.CurrentBonus == Bonuses.Cherry)
            {
                e.Graphics.DrawImage(new Bitmap(Environment.GetFullFileName(@"Images/cherry.png")), bonus.X, bonus.Y);
                return;
            }
            if (bonus.CurrentBonus == Bonuses.Pineapple)
            {
                e.Graphics.DrawImage(new Bitmap(Environment.GetFullFileName("Images/pineapple.png")), bonus.X, bonus.Y);
                return;
            }
            if (bonus.CurrentBonus == Bonuses.Apple)
            {
                e.Graphics.DrawImage(new Bitmap(Environment.GetFullFileName("Images/apple.png")), bonus.X, bonus.Y);
                return;
            }
            if (bonus.CurrentBonus == Bonuses.Banana)
            {
                e.Graphics.DrawImage(new Bitmap(Environment.GetFullFileName("Images/banana.png")), bonus.X, bonus.Y);
                return;
            }
            if (bonus.CurrentBonus == Bonuses.Strawberry)
            {
                e.Graphics.DrawImage(new Bitmap(Environment.GetFullFileName("Images/strawberry.png")), bonus.X, bonus.Y);
                return;
            }
        }
    }
}
